package com.ymatou.iapi.productmanager.testcase;

import com.ymatou.iapi.productmanager.parameter.CheckProductExistsGrouponBean;
import com.ymatou.iapi.productmanager.parameter.SaveGrouponBean;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productmanager.service.CheckProductExistsGrouponCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.database.model.GrouponProducts;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.util.List;

/**
*检查商品是否在参加拼团
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_CheckProductExistsGroupon{
	private static CheckProductExistsGrouponBean checkproductexistsgrouponBean;
	private static CheckProductExistsGrouponCall checkproductexistsgrouponCall;
	private static YmtProductsIWapper ymtProductsIWapper;
	/**
	 * 优选
	 */
	private static Integer sellerId = 3383;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("检查商品是否在参加拼团");
	}

	@Before
	public void caseUp() {
		checkproductexistsgrouponBean=new CheckProductExistsGrouponBean();
		checkproductexistsgrouponCall=new CheckProductExistsGrouponCall();
		ymtProductsIWapper = new YmtProductsIWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(checkproductexistsgrouponCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void setStock(AddSellerProductRequest addpro, Integer catalogNum) {
		addpro.getProduct().getCatalogList().forEach(catalog -> {
			catalog.setStock(catalogNum);
		});
	}

	private void setPrice(AddSellerProductRequest addpro, Double price) {
		addpro.getProduct().getCatalogList().forEach(catalog -> {
			catalog.setNewPrice(price);
			catalog.setMarketPrice(price);
			catalog.setVipPrice(price);
			catalog.setPrice(price);
		});
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CheckProductExistsGroupon_01() {
		Logger.start(true,"正在拼团商品检查是否在参加拼团");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			setStock(addpro, 30);
			setPrice(addpro, 100d);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);
			SaveGrouponBean saveGrouponBean = ProductManagerCallServiceV3.initSaveGrouponBean(sellerId,productId,80d);
			ProductManagerCallServiceV3.SaveGrouponCall(saveGrouponBean);
			checkproductexistsgrouponBean.setProductId(productId);
			checkproductexistsgrouponCall.setData(checkproductexistsgrouponBean);
			checkproductexistsgrouponCall.callService();
			Logger.verifyEquals("200",checkproductexistsgrouponCall.getString("Code"),"验证Code");
			Logger.verifyEquals(true,checkproductexistsgrouponCall.getJsonObject().get("Data").getAsJsonObject().get("InGroupon").getAsBoolean(),"验证是否参团");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CheckProductExistsGroupon_02() {
		Logger.start(true,"停止拼团商品检查是否在参加拼团");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			setStock(addpro, 30);
			setPrice(addpro, 100d);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);
			SaveGrouponBean saveGrouponBean = ProductManagerCallServiceV3.initSaveGrouponBean(sellerId,productId,80d);
			ProductManagerCallServiceV3.SaveGrouponCall(saveGrouponBean);
			checkproductexistsgrouponBean.setProductId(productId);
			checkproductexistsgrouponCall.setData(checkproductexistsgrouponBean);
			checkproductexistsgrouponCall.callService();
			Logger.verifyEquals("200",checkproductexistsgrouponCall.getString("Code"),"验证Code");
			Logger.verifyEquals(true,checkproductexistsgrouponCall.getJsonObject().get("Data").getAsJsonObject().get("InGroupon").getAsBoolean(),"验证是否参团");
			List<GrouponProducts> gp = ymtProductsIWapper.selectGrouponProducts(productId, null, 0);

			ProductManagerCallServiceV3.StopGroupon(gp.get(0).getGrouponProductId());
			checkproductexistsgrouponCall.callService();
			Logger.verifyEquals("200",checkproductexistsgrouponCall.getString("Code"),"验证Code");
			Logger.verifyEquals(false,checkproductexistsgrouponCall.getJsonObject().get("Data").getAsJsonObject().get("InGroupon").getAsBoolean(),"验证是否参团");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CheckProductExistsGroupon_03() {
		Logger.start(true,"开团停团再开团商品检查是否在参加拼团");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			setStock(addpro, 30);
			setPrice(addpro, 100d);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);
			checkproductexistsgrouponBean.setProductId(productId);
			checkproductexistsgrouponCall.setData(checkproductexistsgrouponBean);
			checkproductexistsgrouponCall.callService();
			Logger.verifyEquals("200",checkproductexistsgrouponCall.getString("Code"),"验证Code");
			Logger.verifyEquals(false,checkproductexistsgrouponCall.getJsonObject().get("Data").getAsJsonObject().get("InGroupon").getAsBoolean(),"验证是否参团");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

}